package com.tledu.data.imports.util;

import lombok.extern.slf4j.Slf4j;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

@Slf4j
public class MyDateUtil {
    public String getCurrentDate(Object date, Object time) {
        // 处理日期字段
        String dateString = (String) date;  // 日期字段，假设格式为 "星期六, 一月 02, 2021"
        // 处理时间字段
        String timeString = (String) time;  // 时间字段，假设格式为 "8:00"

        dateString = dateString.replaceAll("星期[一二三四五六日]+,", "").trim();

        dateString = dateString.replace("一月", "01")
                .replace("二月", "02")
                .replace("三月", "03")
                .replace("四月", "04")
                .replace("五月", "05")
                .replace("六月", "06")
                .replace("七月", "07")
                .replace("八月", "08")
                .replace("九月", "09")
                .replace("十月", "10")
                .replace("十一月", "11")
                .replace("十二月", "12");

        String loacltime = dateString.trim()+" "+timeString;

        // 解析原始时间字符串
        DateFormat inputFormat = new SimpleDateFormat("MM dd, yyyy HH:mm");
        Date parsedDate = null;
        try {
            parsedDate = inputFormat.parse(loacltime);
        } catch (Exception e) {
            e.printStackTrace();
//            continue; // 如果解析失败，跳过该行
        }

        // 格式化为目标格式
        DateTimeFormatter outputFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime localDateTime = parsedDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        String formattedDateTime = localDateTime.format(outputFormat);
        return formattedDateTime;
    }

    public String getDate(Object date){
        // 处理日期字段
        String dateString = (String) date;  // 日期字段，假设格式为 "星期六, 一月 02, 2021"

        dateString = dateString.replaceAll("星期[一二三四五六日]+,", "").trim();

        dateString = dateString.replace("一月", "01")
                .replace("二月", "02")
                .replace("三月", "03")
                .replace("四月", "04")
                .replace("五月", "05")
                .replace("六月", "06")
                .replace("七月", "07")
                .replace("八月", "08")
                .replace("九月", "09")
                .replace("十月", "10")
                .replace("十一月", "11")
                .replace("十二月", "12");

        String loacltime = dateString.trim();

        // 解析原始时间字符串
        DateFormat inputFormat = new SimpleDateFormat("MM dd, yyyy");
        Date parsedDate = null;
        try {
            parsedDate = inputFormat.parse(loacltime);
        } catch (Exception e) {
            e.printStackTrace();
//            continue; // 如果解析失败，跳过该行
        }

        // 格式化为目标格式
        DateTimeFormatter outputFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime localDateTime = parsedDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        String formattedDateTime = localDateTime.format(outputFormat);
        return formattedDateTime;
    }
}
